new Vue({
  el: '#app',
  data: {
    ossImgUrl: ossImgUrl,//oss图片域名
    env: '',//判断环境
    type: "",//1 个人签约, 2 门店商户签约
    tel: "",//用户手机尾号
    flow_id: "",//注册代办人时返回的ID
    user_number: "",//用户证件号
    code: "",//短信验证码
    length: 6,//验证码的长度
    focus: false,
    disabled: false,//验证码输入框是否可输入
    djsTime: 119,//倒计时时间
    timer: ''//计时器
  },
  computed: {
    arrCode: function () {
      return this.code.split('');
    },
    currentIndex: function () {
      return this.code.length;
    }
  },
  watch: {
    code: function (newV, oldV) {
      var self = this;
      this.code = newV.replace(/[^\d]/g, ''); // 限制非数字
      if (newV.length >= 6) {
        this.disabled = true;
        this.$refs.code.blur();
        setTimeout(function () {
          // 发送短信验证码
          self.sendCode();
        }, 500);
      }
    }
  },
  created() {
    // 判断是否在小程序内
    var u = window.navigator.userAgent;
    var that = this;
    if (/MicroMessenger/i.test(u)) {//判断是否是微信环境
      wx.miniProgram.getEnv(res => {
        if (res.miniprogram) {// 小程序
          that.env = 'mp';
        }
      })
    }
    // 获取缓存签约类型
    this.type = local.get("type");
  },
  mounted() {
    clearInterval(this.timer);
    this.flow_id = getUrlParam("flow_id");
    this.user_number = getUrlParam("user_number");
    var tel = getUrlParam("user_phone");
    this.tel = tel.substr(tel.length - 4)
    this.djs();
  },
  destroyed() {
    clearInterval(this.timer);
  },
  methods: {
    // 倒计时
    djs: function () {
      var that = this;
      this.timer = setInterval(function () {
        that.djsTime--;
        if (that.djsTime <= 0) {
          that.disabled = false;
          that.code = "";
          clearInterval(that.timer);
        }
      }, 1000)
    },
    // 验证码提交
    sendCode: function () {
      var that = this;
      post("api/agency/code/auth", {
        flow_id: this.flow_id,
        code: this.code
      }).then(function (res) {
        if (that.type == 1) {//个人签约->签协议
          window.location.href = "sign.html"
        } else if (that.type == 2) {//企业签->法人授权书认证-发送
          window.location.href = "legalRz_fs.html?user_phone=" + that.tel;
        }
      })
    },
    // 重新发送验证码
    sendCodeAgain: function () {
      this.djsTime = 119;
      this.djs();
      var that = this;
      put("api/enp/" + this.user_number + "/user").then(function (res) {
        if (res.data && res.data.flow_id) {
          that.flow_id = res.data.flow_id;
        }
      })
    }
  },
})